package com.stox.client.ui.javafx.screener.widget;

import java.util.List;

import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.control.PopupControl;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;

import com.stox.client.api.core.Client;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.client.ui.javafx.screener.view.ScreenerView;
import com.stox.shared.screener.model.ScreenBeanDescriptor;

public class ScreenChooserPopupControl extends PopupControl {

	private final FlowPane contentPane = new FlowPane();
	private final ScreenerView screenerView;
	private final EventHandler<MouseEvent> eventHandler = new EventHandler<MouseEvent>(){
		@Override
		public void handle(MouseEvent event) {
			Label label = (Label)event.getSource();
			ScreenBeanDescriptor screenBeanDescriptor = (ScreenBeanDescriptor)label.getUserData();
			screenerView.addScreenBeanDescriptor(screenBeanDescriptor);
			hide();
		}
	};
	
	public ScreenChooserPopupControl(ScreenerView screenerView) {
		super();
		this.screenerView = screenerView;
		UI.style(contentPane, CSS.stoxPopup, CSS.chooserPopupContentPane);
		setAutoHide(true);
		centerOnScreen();
		getScene().setRoot(contentPane);

		List<ScreenBeanDescriptor> screenBeanDescriptors = Client.SCREENER.findAll();
		for(ScreenBeanDescriptor screenBeanDescriptor : screenBeanDescriptors){
			Label label = new Label(screenBeanDescriptor.getDisplayName());
			UI.style(label, CSS.buttonMockingLabel);
			label.setUserData(screenBeanDescriptor);
			label.setOnMousePressed(eventHandler);
			contentPane.getChildren().add(label);
		}
	}
	
}
